/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package presenter;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Al
 */
public class Videos extends JPanel implements KeyListener, MouseListener{
    
    SongFile song;
    JList videos;
    JButton refreshList;
    File videoDirectory;
    JScrollPane scrollPane;

    boolean update;
    
    
    public Videos(File videoDirectory) {
        super(new BorderLayout());
        song=new SongFile("Nothing");
        this.videoDirectory=videoDirectory;
        videos=new JList();
        loadVideos();
        update=false;
        scrollPane = new JScrollPane();
        scrollPane.getViewport().setView(videos);
        add(scrollPane,"Center");
        videos.addKeyListener(this);
        videos.addMouseListener(this);
    }
    
    public SongFile getSong() {
        return (SongFile)videos.getSelectedValue();
    }    
    private void loadVideos() {
        if(videoDirectory.isDirectory()) {
            File files[] = videoDirectory.listFiles();
            ArrayList<SongFile> vids=new ArrayList<SongFile>();
            for(int i=0; i<files.length; i++) {
                String name=files[i].getName().substring(0, files[i].getName().indexOf("."));
                
                SongFile temp=new SongFile(name);
                temp.setVideo(files[i].getPath());
                vids.add(temp);
            }
            videos.setListData(vids.toArray(new SongFile[1]));
        }
    }

    public void mouseClicked(MouseEvent e) {
        if(e.getClickCount()==2) {
            update=true;
        }
    }

    public void mousePressed(MouseEvent e) {
        
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        
    }

    public void mouseExited(MouseEvent e) {
        
    }

    public void keyTyped(KeyEvent e) {
        
    }

    public void keyPressed(KeyEvent e) {
        System.out.println(e.getModifiers()+" "+e.getKeyCode());
        if(e.getModifiers()==0&&(e.getKeyCode()==10))
            update=true;
    }

    public void keyReleased(KeyEvent e) {
        
    }
    
    public boolean isUpdated()
    {
        if(update)
        {
            update = false;
            return true;
        } else
        {
            return false;
        }
    }

}
